const app = getApp();
const companyName = app.globalData.companyName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coachList: []
  },

  onLoad: function(options) {
    this.fetchCoachList();
  },
  fetchCoachList: function() {
    const baseUrl = app.globalData.baseUrl;
    const that = this;
    wx.showNavigationBarLoading();
    wx.request({
      url: app.globalData.url + '/coach/coachList',
      data: {
        companyName
      },
      method: 'POST',
      success: function(response) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (response.data.success) {
          const content = response.data.content;
          content.forEach(function(item) {
            item.fitnessPicSrc = baseUrl + '/file/' + item.fitnessPicSrc + '?number=' + Math.random();
          });
          that.setData({
            coachList: content
          });
        } else {
          wx.showToast({
            title: '获取教练列表出错',
            icon: 'none'
          })
        }
      }
    });
  },
  onPullDownRefresh: function() {
    this.fetchCoachList();
  },
  toDetail: function(e) {
    const index = e.currentTarget.dataset.index;
    const openId = this.data.coachList[index].openId;
    wx.setStorageSync("openId", openId);
    wx.navigateTo({
      url: '/pages/detail/detail',
    })
  },
})